<template>
    <div class="addOrEdit">
        <el-dialog
            :before-close="handClose"
            center
            width="30%"
            title="角色信息修改和添加"
            :visible.sync="off"
            append-to-body>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini" class="demo-ruleForm">
                <el-form-item label="角色名" prop="roleName">
                    <el-input v-model.trim="ruleForm.roleName"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="create">立即创建</el-button>
            </div>
        </el-dialog>
    </div>
   
</template>

<script>
    import { addRole, editRole } from "@/api/role.js"
    export default {
        name: "addOrEdit",
        props: {
            off: {
                type: Boolean
            },
            rowData: {
                type: Object
            },
            type: {
                type: String
            },
        },
        watch: {
            off(val){
                if(val){
                    if(this.type == "add"){
                        this.$nextTick(() => {
                            this.$refs.ruleForm.resetFields();
                        });
                    }
                    if(this.type == "edit"){
                        this.ruleForm.roleName = this.rowData.roleName;
                    }
                }
            }
        },
        data(){
            return {
                rules: {
                     roleName: [
                        {
                            required: true,
                            message: "角色名称不能为空",
                            trigger: "blur"
                        }
                    ]
                },
                ruleForm: {
                    roleName: "",
                }
            }
        },
        methods: {
            handClose(){
                this.$emit("update:off", false);
            },
            create(){
                this.$refs.ruleForm.validate((valid) => {
                    if(!valid){
                        return;
                    }else{
                        // var obj = {
                        //     permissionName: this.ruleForm.permissionName,
                        //     url: this.ruleForm.url
                        // }
                        if(this.type == "edit"){
                            editRole({
                                createDate: this.rowData.createDate,
                                descript: this.rowData.createDate,
                                roleId: this.rowData.roleId,
                                roleName: this.ruleForm.roleName,
                                token: this.rowData.token,
                                updateDate: this.rowData.updateDate,
                                userId: this.rowData.userId
                            }).then(
                                res => {
                                    this.$message({
                                        type: "success",
                                        message: "修改成功"
                                    });
                                    this.$emit("update:off", false);
                                    this.$parent.getRoleList();
                                    this.$refs.ruleForm.resetFields();
                                },
                                err => {}
                            );
                        }else{
                            addRole({
                                roleName: this.ruleForm.roleName
                            }).then(
                                res => {
                                    console.log("res新增角色", res);
                                    this.$message({
                                        type: "success",
                                        message: "新增角色成功"
                                    });
                                    this.$emit("update:off", false);
                                    this.$parent.getRoleList();
                                    this.$refs.ruleForm.resetFields();
                                },
                                err => {}
                            );
                        }
                    }
                });
            }
        }
    }
</script>

<style scoped lang="less">

</style>